package com.loong.ems.controller;

import com.loong.ems.domain.entity.EmpLeaveRecord;
import com.loong.ems.domain.vo.resp.EmpLeaveRecordVo;
import com.loong.ems.domain.vo.resp.Result;
import com.loong.ems.service.EmpLeaveRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 龙勇-229970722
 * @date 2025-01-03 14:36
 * @description: 请假管理
 */
@RestController
@RequestMapping("/leave")
@Log4j2
@CrossOrigin
@Tag(name = "请假信息管理 API" , description = "请假信息管理 API")
public class EmpLeaveRecordController {

    @Autowired
    private EmpLeaveRecordService empLeaveRecordService;

    /**
     * 获取请假信息列表
     * @return
     */
    @Operation(summary = "获取请假信息列表")
    @GetMapping("/list")
    public Result<List<EmpLeaveRecordVo>> getLeaveRecordList() {
        return Result.success(empLeaveRecordService.getLeaveRecordList());
    }
    @Operation(summary = "删除请假信息")
    @GetMapping("/delete/{id}")
    public Result<String> deleteLeaveRecord(@PathVariable Long id) {
        int i = empLeaveRecordService.deleteLeaveRecord(id);
        if (i == 0) {
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }
    /**
     * 获取请假信息待审批列表
     * @return
     */
    @Operation(summary = "获取请假信息待审批列表")
    @GetMapping("/pending")
    public Result<List<EmpLeaveRecordVo>> getPendingLeaveRecordList() {
        return Result.success(empLeaveRecordService.getPendingLeaveRecordList());
    }

    /**
     * 获取代销假列表
     * @return
     */
    @Operation(summary = "获取代销假列表")
    @GetMapping("/plc")
    public Result<List<EmpLeaveRecordVo>> getPendingLeaveCancelRecordList() {
        return Result.success(empLeaveRecordService.getPendingLeaveCancelRecordList());
    }
    /**
     * 添加请假信息
     */
    @Operation(summary = "添加请假信息")
    @PostMapping("/add")
    public Result<String> addLeaveRecord(@RequestBody EmpLeaveRecord empLeaveRecord) {
        int i = empLeaveRecordService.addLeaveRecord(empLeaveRecord);
        if (i == 0) {
            return Result.error("添加失败");
        } else if (i == -1) {
            return Result.error("请假时间不能早于结束时间");
        }
        return Result.success("添加成功");

    }

    /**
     * 更新请假信息
     * @param empLeaveRecord
     * @return
     */
    @Operation(summary = "更新请假信息")
    @PostMapping("/update")
    public Result<String> updateLeaveRecord(@RequestBody EmpLeaveRecord empLeaveRecord) {
        int i = empLeaveRecordService.updateLeaveRecord(empLeaveRecord);
        if (i == 0) {
            return Result.error("更新失败");
        } else if (i == -1) {
            return Result.error("请假时间不能早于结束时间");
        }
        return Result.success("更新成功");
    }

    /**
     * 根据用户id获取请假信息数量
     * @param userId
     * @return
     */
    @Operation(summary = "根据用户id获取请假信息数量")
    @GetMapping("/person/{id}")
    public Result<List<EmpLeaveRecordVo>> personLeave(@PathVariable(value = "id") Long userId) {
        return Result.success(empLeaveRecordService.personLeave(userId));
    }

}
